package cn.topcodes.tcsf.app.service.impl;

import cn.topcodes.tcsf.admin.commons.domain.Page;
import cn.topcodes.tcsf.admin.commons.generic.GenericService;
import cn.topcodes.tcsf.app.domain.entity.Notice;
import cn.topcodes.tcsf.app.domain.form.NoticeForm;
import cn.topcodes.tcsf.app.service.NoticeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @author Unicorn
 * @date 2017/5/5
 */
@Service("noticeService")
@Transactional(readOnly = true)
public class NoticeServiceImpl extends GenericService<Notice> implements NoticeService {

    /**
     * 获取最新APP公告
     *
     * @return 公告
     */
    @Override
    public Notice getLastNotice() {
        Example example = this.buildExample();
        example.setOrderByClause("id desc");
        return this.findOne(example);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void insertNotice(NoticeForm noticeForm, Long userId) {
        Notice notice = new Notice();
        notice.setTitle(noticeForm.getTitle());
        notice.setType(noticeForm.getType());
        notice.setContent(noticeForm.getContent());
        notice.setSysUserId(userId);
        this.save(notice);
    }

    /**
     * 分页列表数据
     *
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @Override
    public Page<Notice> findPageBy(Integer pageNumber, Integer pageSize) {
        Example example = this.buildExample();
        example.setOrderByClause("created_time desc");
        return this.findPage(example, pageNumber, pageSize);
    }
}
